﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAO;
using DTO;
using BUS;


namespace QLBANSACH
{
    public partial class Sach : Form
    {
        SqlConnection connection = DAO.sqlConnectionData.Hamketnoi();

        public Sach()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Sach_DTO cthdn = new Sach_DTO(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox3.Text), textBox4.Text, Convert.ToInt32(textBox5.Text), textBox6.Text, Convert.ToInt32(textBox7.Text));
                Sach_BUS.InsertSACH(cthdn);
                dataGridView1.DataSource = Sach_BUS.Hienthisach();
                databinding();
                MessageBox.Show("Thêm thành công!");
            }
            catch
            {
                MessageBox.Show("Thêm thất bại!");
                dataGridView1.DataSource = Sach_BUS.Hienthisach();
                databinding();
            }
        }

        public void databinding()
        {
            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add("Text", dataGridView1.DataSource, "ID_Sach");
            textBox2.DataBindings.Clear();
            textBox2.DataBindings.Add("Text", dataGridView1.DataSource, "ID_TLS");
            textBox3.DataBindings.Clear();
            textBox3.DataBindings.Add("Text", dataGridView1.DataSource, "ID_NXB");
            textBox4.DataBindings.Clear();
            textBox4.DataBindings.Add("Text", dataGridView1.DataSource, "Ten_Sach");
            textBox5.DataBindings.Clear();
            textBox5.DataBindings.Add("Text", dataGridView1.DataSource, "Gia_Sach");
            textBox6.DataBindings.Clear();
            textBox6.DataBindings.Add("Text", dataGridView1.DataSource, "TrangThai");
            textBox7.DataBindings.Clear();
            textBox7.DataBindings.Add("Text", dataGridView1.DataSource, "SL_SP");
        }

        private void Sach_Load(object sender, EventArgs e)
        {
            connection.Open();
            dataGridView1.DataSource = Sach_BUS.Hienthisach();
            databinding();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Sach_DTO cthdn = new Sach_DTO(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox3.Text), textBox4.Text, Convert.ToInt32(textBox5.Text), textBox6.Text, Convert.ToInt32(textBox7.Text));
                Sach_BUS.UpdateSACH(cthdn);
                dataGridView1.DataSource = Sach_BUS.Hienthisach();
                databinding();
                MessageBox.Show("Sửa thành công!");
            }
            catch
            {
                MessageBox.Show("Sửa thất bại!");
                dataGridView1.DataSource = Sach_BUS.Hienthisach();
                databinding();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Sach_DTO cthdn = new Sach_DTO(textBox1.Text);
                Sach_BUS.DeleteSACH(cthdn);
                dataGridView1.DataSource = Sach_BUS.Hienthisach();
                databinding();
                MessageBox.Show("Xóa thành công!");
            }
            catch
            {
                MessageBox.Show("Xóa thất bại!");
                dataGridView1.DataSource = Sach_BUS.Hienthisach();
                databinding();
            }
        }
    }
}
